package com.xcity.game.worldevent;

import com.alibaba.fastjson.annotation.JSONField;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;

public class WorldEvent {

	protected int id; // 事件id
	protected long time; // 事件发生时间
	
	@JSONField(serialize = false)
	protected WorldEventTemplate ref;
	
	public WorldEvent() {}
	
	public WorldEvent(WorldEventTemplate ref) {
		this.id = ref.getId();
		this.time = TimeUpdater.getInstance().now();
		this.ref = ref;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public WorldEventTemplate getRef() {
		return ref != null ? ref : (ref = App.getApp().getServiceManager().get(TemplateService.class).get(WorldEventTemplate.class, id));
	}

	public void setRef(WorldEventTemplate ref) {
		this.ref = ref;
	}
	
	@JSONField(serialize = false)
	public boolean isGood() {
		return getRef().getType() == WorldEventTemplate.GOOD;
	}
	
	public boolean validate() {
		return getRef().getDuration() * 1000 + time > TimeUpdater.getInstance().now();
	}
	
	@JSONField(serialize = false)
	public long getEndTime() {
		return time + getRef().getDuration() * 1000;
	}
	
}
